package jdbc.three.transaction.impl;

import jdbc.three.transaction.TransactionManager;
import tmp.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class TransactionManagerImpl implements TransactionManager {
    @Override
    public void beginTransaction() throws SQLException {
        Connection con=null;
        try {
            con= DBUtil.getConnection();
            con.setAutoCommit(false);
        } catch (SQLException e) {
            throw new SQLException("开启事务失败");
        }
    }

    @Override
    public void commit() throws SQLException {
        try {
            Connection con= DBUtil.getConnection();
            con.commit();
            con.setAutoCommit(true);
            DBUtil.close(con,null,null);
        } catch (SQLException e) {
            throw new SQLException("事务提交失败");
        }
    }

    @Override
    public void rollback() throws SQLException {
        try {
            Connection con= DBUtil.getConnection();
            con.rollback();
            con.setAutoCommit(true);
            DBUtil.close(con,null,null);

        } catch (SQLException e) {
            throw new SQLException("事务回滚失败");
        }
    }
}
